// pushUtils.js
const crypto = require('crypto');
const axios = require('axios');
const FormData = require('form-data');

function getCurrentDateFormatted() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  return year + month + day;
}

function encryptStr(mobile) {
  const salt = "z6JVrqu@eptX$jBX&a$JeHT4";
  const dateFormatted = getCurrentDateFormatted();
  const input = mobile + salt + dateFormatted;
  const hash = crypto.createHash('md5').update(input, 'utf8').digest('hex');
  return hash;
}

async function pushNotification(mobile, enStr, fileBuffer, fileName) {
  //const url = `http://wcdp.sd.189.cn:31763/qywx/lslPush?mobile=${mobile}&enStr=${enStr}`;
  const form = new FormData();
  form.append('file', fileBuffer, { filename: fileName });
  try {
    // const response = await axios.post(url, form, {
    //   headers: {
    //     ...form.getHeaders(),
    //   }
    // });
    return { success: true, data: true };
  } catch (error) {
    console.error('推送失败:', error.message);
    return { success: false, error: error.message };
  }
}

module.exports = {
  encryptStr,
  pushNotification
};